<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px">
          <el-col :span="5">
            <el-form-item
              v-if="this.$store.state.user.fsiteLevel == 3"
              label="客户名称"
            >
              <el-input
                :value="this.$store.state.user.fsiteName"
                v-model.trim="listQuery.fsiteName"
                disabled
                clearable
                class="filter-item"
                placeholder="请输入客户名称"
              />
            </el-form-item>
            <el-form-item v-else label="客户名称">
              <el-input
                v-model.trim="listQuery.fsiteName"
                clearable
                class="filter-item"
                placeholder="请输入客户名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="所属地区">
              <el-select
                v-model="listQuery.fsiteParent"
                clearable
                filterable
                class="filter-item"
                placeholder="请选择所属地区"
              >
                <el-option
                  v-for="item in fsiteNoBelongOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleFilter"
                >查询</el-button
              >
              <el-button
                type="success"
                icon="el-icon-plus"
                @click="handleCreate"
                >新增
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <JNPF-table :data="list" @row-click="clickRow">
          <el-table-column :width="buttonColor.operationTwo" align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <el-button type="text" @click="handleUpdate(scope.row)"> 编辑</el-button>
            <el-button type="text" @click="handleDelete(scope.row)"> 删除</el-button>
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                更多<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown" trigger="click">
                <el-dropdown-item command="a">预约</el-dropdown-item>
                <el-dropdown-item command="b">物料</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </template>
      </el-table-column>
          <el-table-column
            :width="buttonColor.billno"
            align="left"
            label="客户编号"
            prop="fsiteNo"
          />
          <el-table-column
            align="left"
            label="客户名称"
            prop="fsiteName"
            width="180"
          />
          <el-table-column
            :formatter="formatSite"
            align="left"
            label="所属地区"
            prop="fsiteParent"
          />
          <el-table-column
            :width="buttonColor.name"
            align="left"
            label="联系人"
            prop="flinkMan"
          />
          <el-table-column
            width="120"
            align="left"
            label="手机"
            prop="fphone"
          />
          <el-table-column
            :width="buttonColor.name"
            :formatter="formatfengineer"
            align="left"
            label="工程师"
            prop="fengineer"
          />
          <el-table-column
            :width="buttonColor.state"
            align="left"
            label="客户状态"
            prop="fsiteState"
          >
            <template slot-scope="scope">
              <span
                v-if="scope.row.fsiteState == 0"
                :style="{ color: buttonColor.statedisable }"
                >{{ scope.row.fsiteState | statusFilter }}</span
              >
              <span v-else :style="{ color: buttonColor.statestart }">{{
                scope.row.fsiteState | statusFilter
              }}</span>
            </template>
          </el-table-column>
        </JNPF-table>
        <paginations
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
        />
      </div>
    </div>
    <!-- 添加或修改对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      width="50%"
      :title="textMap[dialogStatus]"
      top="10px"
    >
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="dataForm"
          status-icon
          label-position="right"
          label-width="80px"
        >
          <el-col :span="12">
            <el-form-item label="客户编号" prop="fsiteNo">
              <el-input
                v-model="dataForm.fsiteNo"
                :disabled="dialogStatus == 'update'"
                type="number"
              /><!-- oninput="value=value.replace(/[^\d]/g,'')"-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属地区" prop="fsiteParent">
              <el-select
                v-model="dataForm.fsiteParent"
                clearable
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in fsiteNoBelongOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="fsiteName">
              <el-input v-model="dataForm.fsiteName" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              class="el-form-item-small"
              label="客户类型"
              prop="fsiteLevel"
            >
              <el-select
                :disabled="true"
                v-model="dataForm.fsiteLevel"
                class="filter-item"
                placeholder="请选择地区类型"
              >
                <el-option
                  v-for="(key, value) in siteTypeMap"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="fsiteL">
              <el-input v-model="dataForm.fsiteL" @focus="clickMap" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="fsiteD">
              <el-input v-model="dataForm.fsiteD" @focus="clickMap" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="flinkMan">
              <el-input v-model="dataForm.flinkMan" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机" prop="fphone">
              <el-input v-model="dataForm.fphone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="ftelno">
              <el-input v-model="dataForm.ftelno" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="femail">
              <el-input v-model="dataForm.femail" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程师" prop="fengineer">
              <el-select
                v-model="dataForm.fengineer"
                clearable
                filterable
                class="filter-item"
                placeholder="请选择工程师"
              >
                <el-option
                  v-for="item in getListfengineer"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              class="el-form-item-small"
              label="状态"
              prop="fsiteState"
            >
              <el-select
                v-model="dataForm.fsiteState"
                class="filter-item"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="(key, value) in statusMap"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              class="el-form-item-small"
              label="客户行业"
              prop="fsiteType"
            >
              <el-select
                v-model="dataForm.fsiteType"
                class="filter-item"
                placeholder="请选择客户行业"
              >
                <el-option
                  v-for="item in fsiteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="休息日" prop="fexecweekList">
              <el-select
                v-model="dataForm.fexecweekList"
                clearable
                multiple
                placeholder="请选择"
              >
                <el-option
                  :value="null"
                  label="全选"
                  @click.native="selectAll"
                />
                <el-option
                  v-for="(key, value) in fexecweekOptions"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上班时间" prop="fbegtime">
              <el-time-select
                v-model="dataForm.fbegtime"
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:30',
                }"
                placeholder="上班时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下班时间" prop="fendtime">
              <el-time-select
                v-model="dataForm.fendtime"
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:30',
                }"
                placeholder="下班时间"
              />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="createData" v-if="dialogStatus =='create'">确 定</el-button>
        <el-button type="primary" @click="updateData" v-else>修 改</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisibleMaterial"
      :title="textMap[dialogStatus]"
      width="50%"
    >
    <el-row class="JNPF-common-search-box" :gutter="16">
      <el-form
        ref="dataFormMaterial"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="90"
      >
          <el-col :span="12">
            <el-form-item  label="客户编号" prop="fsiteNo">
              <el-input
                v-model="dataForm.fsiteNo"
                disabled
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="客户名称"
              prop="fsiteName"
            >
              <el-input v-model="dataForm.fsiteName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              class="el-form-item-small"
              label="物料预约"
              prop="fusetype"
            >
              <el-select
                v-model="dataForm.fusetype"
                class="filter-item"
                placeholder="请选择物料预约"
              >
                <el-option
                  v-for="(key, value) in statusMap"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="柜子授权"
              prop="fboxflag"
            >
              <el-checkbox-group v-model="fboxflag">
                <el-checkbox label="用户只能操作授权的柜子" name="fboxflag"/>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              class="el-form-item-small"
              label="盘点预约"
              prop="finvtype"
            >
              <el-select
                v-model="dataForm.finvtype"
                class="filter-item"
                placeholder="请选择盘点预约"
              >
                <el-option
                  v-for="(key, value) in statusMap"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              class="el-form-item-small"
              label="出库预约"
              prop="fouttype"
            >
              <el-select
                v-model="dataForm.fouttype"
                class="filter-item"
                placeholder="请选择出库预约"
              >
                <el-option
                  v-for="(key, value) in statusMap"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
      </el-form>
    </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleMaterial = false">取 消</el-button>
        <el-button type="primary" @click="updateDataMaterial">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 预约 -->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisibleOrder"
      width="50%"
      :title="textMap[dialogStatus]"
    >
    <el-row class="JNPF-common-search-box" :gutter="16">
      <el-form
        ref="dataFormOrder"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="110px"
      >
          <el-col :span="12">
            <el-form-item  label="客户编号" prop="fsiteNo">
              <el-input
                v-model="dataForm.fsiteNo"
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="客户名称"
              prop="fsiteName"
            >
              <el-input v-model="dataForm.fsiteName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="门禁URL"
              prop="fappurl"
            >
              <el-input v-model="dataForm.fappurl" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="门禁APPID"
              prop="fappid"
            >
              <el-input v-model="dataForm.fappid" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="门禁APPKEY"
              prop="fappkey"
            >
              <el-input v-model="dataForm.fappkey" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="门禁AppSecret"
              prop="fappsecret"
            >
              <el-input v-model="dataForm.fappsecret" />
            </el-form-item>
          </el-col>
      </el-form>
    </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleOrder = false">取 消</el-button>
        <el-button type="primary" @click="updateDataOrder">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 地图 -->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible2"
      fullscreen
      width="1200px"
      height="800px"
      style="width: 100%; height: 80%; padding-top: 0px"
    >
      <template slot="title">
        <div :style="titleColor">百度地图</div>
      </template>
      <div
        slot="title"
        align="right"
        style="margin-top: 10px; margin-right: 30px"
      >
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" round @click="dialogFormVisible2 = false">取消</el-button>
        <el-button :style="{background:buttonColor.confirm,color:buttonColor.color}" round @click="setLatLng">确定</el-button>
      </div>
      <cmap
        :center="dataForm.fsiteParent"
        :keyword="dataForm.fsiteName"
        :value="dialogFormVisible2"
        style="margin-top: 0px; width: 100%"
        @transfer="getPoint"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listArea,
  createArea,
  updateArea,
  deleteArea,
  fsiteOptionsByLevel,
  getListArea,
} from "@/api/area";
import moment from "moment";
import Pagination from "@/components/Pagination";
import { uploadPath } from "@/api/room";
import cmap from "./baiduMap";
import { tableColor, titleColor, buttonColor } from "@/filters/index";

const siteTypeMap = {
  0: "省",
  1: "市",
  2: "区",
  3: "学校",
};

const statusMap = {
  0: "禁用",
  1: "启用",
};
const fsiteTypeMap = {
  1: "教育",
  2: "零售",
};

const fexecweekOptions = {
  1: "周一",
  2: "周二",
  3: "周三",
  4: "周四",
  5: "周五",
  6: "周六",
  7: "周日",
};

export default {
  name: "Area",
  components: { Pagination, cmap },
  filters: {
    siteTypeFilter(type) {
      return siteTypeMap[type];
    },
    statusFilter(type) {
      return statusMap[type];
    },

    fsiteTypeFilter(type) {
      return fsiteTypeMap[type];
    },
  },
  data() {
    return {
      getListfengineer: null,
      uploadPath,
      list: null,
      total: 0,
      fsiteNoBelongOptions: null,
      center: "北京",
      lat: 0,
      lng: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fsiteName: undefined,
        fsiteParent: undefined,
        fengineer: undefined,
        fsiteLevel: undefined,
        sort: "create_time",
        order: "desc",
      },
      siteTypeMap,
      statusMap,
      fsiteTypeMap,
      fexecweekOptions,
      fsiteTypeList: [
        { value: 1, label: "教育" },
        { value: 2, label: "零售" },
      ],
      dataForm: {
        id: undefined,
        fsiteNo: undefined,
        fimage: undefined,
        fimage3d: undefined,
        fsiteName: undefined,
        fsiteParent: undefined,
        fsiteLevel: undefined,
        fsiteL: undefined,
        fsiteD: undefined,
        flinkMan: undefined,
        fphone: undefined,
        ftelno: undefined,
        femail: undefined,
        fengineer: undefined,
        fsiteState: undefined,
        fupddate: undefined,
        foperator: undefined,
        fsiteType: undefined,
        fbegtime: undefined,
        fendtime: undefined,
        fboxflag: undefined,
        fuseflag: undefined,
        fusetype: undefined,
        fouttype: undefined,
        finvtype: undefined,
        fappurl: undefined,
        fappid: undefined,
        fappkey: undefined,
        fappsecret: undefined,
        fexecweekList: [],
      },
      rowdata:{},
      fboxflag: [],
      fuseflag: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogFormVisibleMaterial: false, // 物料弹窗
      dialogFormVisibleOrder: false, // 预约弹窗
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      tableColor: tableColor,
      titleColor: titleColor,
      buttonColor: buttonColor,
      rules: {
        fsiteNo: [
          { required: true, message: "客户编号不能为空", trigger: "blur" },
        ],
        fsiteName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
        ],
        fsiteParent: [
          { required: true, message: "所属地区不能为空", trigger: "blur" },
        ],
        fsiteLevel: [
          { required: true, message: "地区类型不能为空", trigger: "blur" },
        ],
        fsiteL: [{ required: true, message: "纬度不能为空", trigger: "blur" }],
        fsiteD: [{ required: true, message: "经度不能为空", trigger: "blur" }],
        fsiteState: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: [],
      },
    };
  },
  created() {
    this.getList();
    fsiteOptionsByLevel(2)
      .then((response) => {
        this.fsiteNoBelongOptions = response.data.data.list;
      })
      .catch(() => {
        this.fsiteNoBelongOptions = [];
      });
    getListArea({ flag: 8, arg1: 13 })
      .then((response) => {
        this.getListfengineer = response.data.data.list.filter((item) => {
          item.value = item.value.toString();
          return item;
        });
      })
      .catch(() => {
        this.getListfengineer = [];
      });
  },
  methods: {
    handleCommand(e){
      if(e=="a")
      {
        this.order(this.rowdata)
      }
      else
      {
        this.material(this.rowdata)
      }
    },
    clickRow(data){
      this.rowdata=data;
    },
    formatfengineer: function (row, column) {
      if (this.getListfengineer) {
        for (let i = 0; i < this.getListfengineer.length; i++) {
          if (row.fengineer === this.getListfengineer[i].value) {
            return this.getListfengineer[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    handleAvatarSuccess(res, file) {
      // this.dataForm.fimage = URL.createObjectURL(file.raw)
      this.dataForm.fimage = res.data;
      const time = new Date().getTime();
      this.dataForm.fimage = `${this.dataForm.fimage}?time=${time}`;
    },
    formdata2: function (fsiteno) {
      // const formdata = new FormData()
      // formdata.append('fpointno', '123456')
      return { ftype: "2", fsiteno: fsiteno };
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2;

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isLt2M; // isJPG &&
    },
    formatStatus: function (row, column) {
      return row.fsiteState === 1 ? "启用" : "禁用";
    },
    dateFormat(row, column) {
      const date = row[column.property];
      if (date === undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    formatSite: function (row, column) {
      if (this.fsiteNoBelongOptions) {
        for (let i = 0; i < this.fsiteNoBelongOptions.length; i++) {
          if (row.fsiteParent === this.fsiteNoBelongOptions[i].value) {
            return this.fsiteNoBelongOptions[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    getList() {
      this.listLoading = true;
      this.listQuery.fsiteLevel = 3;
      if (this.$store.state.user.fsiteLevel == 3) {
        this.listQuery.fsiteName = this.$store.state.user.fsiteName;
      }
      listArea(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fsiteNo: undefined,
        fimage: undefined,
        fimage3d: undefined,
        fsiteName: undefined,
        fsiteParent: undefined,
        fsiteLevel: undefined,
        fsiteL: undefined,
        fsiteD: undefined,
        flinkMan: undefined,
        fphone: undefined,
        ftelno: undefined,
        femail: undefined,
        fengineer: undefined,
        fsiteState: undefined,
        fupddate: undefined,
        foperator: undefined,
        fsiteType: undefined,
        fbegtime: undefined,
        fendtime: undefined,
        fboxflag: undefined,
        fuseflag: undefined,
        fusetype: undefined,
        fouttype: undefined,
        finvtype: undefined,
        fappurl: undefined,
        fappid: undefined,
        fappkey: undefined,
        fappsecret: undefined,
        fexecweekList: [],
      };
      this.fboxflag = [];
      this.fuseflag = [];
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.dataForm.fsiteState = "1";
      this.dataForm.fsiteLevel = "3";
      this.dataForm.fsiteType = 1;
      this.dataForm.fusetype = "1";
      this.dataForm.finvtype = "1";
      this.dataForm.fouttype = "1";
      this.dataForm.fuseflag = 0;
      if (this.fsiteNoBelongOptions.length > 0) {
        this.dataForm.fsiteParent = this.fsiteNoBelongOptions[1].value;
      }
      if (this.getListfengineer.length > 0) {
        this.dataForm.fenginee = this.getListfengineer[0].value;
      }
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    clickMap() {
      // this.resetForm()
      this.dialogFormVisible2 = true;
      this.lat = 0;
      this.lng = 0;
      // this.dataForm.fsiteState = '1'
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    getPoint(point) {
      this.lat = point.lat;
      this.lng = point.lng;
    },
    setLatLng() {
      this.dialogFormVisible2 = false;
      this.dataForm.fsiteL = this.lat;
      this.dataForm.fsiteD = this.lng;
      this.$refs["dataForm"].validateField("fsiteL");
      this.$refs["dataForm"].validateField("fsiteD");
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          this.dataForm.fboxflag = this.fboxflag.length;
          this.dataForm.fuseflag = this.fuseflag.length;
          createArea(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "添加成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.data,
              });
            });
        }
      });
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.dataForm.fsiteLevel = row.fsiteLevel.toString();
      if (row.fsiteState == 0 || row.fsiteState == 1) {
        this.dataForm.fsiteState = row.fsiteState.toString();
      }
      this.dialogStatus = "update";
      console.log(this.dialogStatus)
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          updateArea(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.fsiteNo === this.dataForm.fsiteNo) {
                  const index = this.list.indexOf(v);
                  this.dataForm.fsiteState = parseInt(this.dataForm.fsiteState);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.msg,
              });
            });
        }
      });
    },
    handleDelete(row) {
      this.$confirm("是否删除该资料", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteArea(row)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "删除成功",
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response.data.data,
            });
          });
      });
    },
    // 预约
    order(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogFormVisibleOrder = true;
      this.dialogStatus = "update";
      this.$nextTick(() => {
        this.$refs["dataFormOrder"].clearValidate();
      });
    },
    updateDataOrder() {
      this.$refs["dataFormOrder"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          updateArea(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.fsiteNo === this.dataForm.fsiteNo) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisibleOrder = false;
              this.$notify.success({
                title: "成功",
                message: "更新成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.msg,
              });
            });
        }
      });
    },
    // 物料
    material(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogFormVisibleMaterial = true;
      this.dialogStatus = "update";

      if (row.fusetype == 0 || row.fusetype == 1) {
        this.dataForm.fusetype = row.fusetype.toString();
      }
      if (row.finvtype == 0 || row.finvtype == 1) {
        this.dataForm.finvtype = row.finvtype.toString();
      }
      if (row.fouttype == 0 || row.fouttype == 1) {
        this.dataForm.fouttype = row.fouttype.toString();
      }

      if (row.fboxflag == 0) {
        this.fboxflag = [];
      } else {
        this.fboxflag = ["用户只能操作授权的柜子"];
      }
      if (row.fuseflag == 0) {
        this.fuseflag = [];
      } else {
        this.fuseflag = ["用户使用前必须预约"];
      }
      this.$nextTick(() => {
        this.$refs["dataFormMaterial"].clearValidate();
      });
    },
    updateDataMaterial() {
      this.$refs["dataFormMaterial"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          this.dataForm.fboxflag = this.fboxflag.length;
          this.dataForm.fuseflag = this.fuseflag.length;
          updateArea(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.fsiteNo === this.dataForm.fsiteNo) {
                  const index = this.list.indexOf(v);
                  this.dataForm.fusetype = parseInt(this.dataForm.fusetype);
                  this.dataForm.finvtype = parseInt(this.dataForm.finvtype);
                  this.dataForm.fouttype = parseInt(this.dataForm.fouttype);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisibleMaterial = false;
              this.$notify.success({
                title: "成功",
                message: "更新成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.msg,
              });
            });
        }
      });
    },
    selectAll() {
      if (this.dataForm.fexecweekList.length < 7) {
        this.dataForm.fexecweekList = [];
        for (var option in fexecweekOptions) {
          this.dataForm.fexecweekList.push(option);
        }
      } else {
        this.dataForm.fexecweekList = [];
      }
    },
    indexMethod(index) {
      index = index + 1;
      return index;
    },
  },
};
</script>